package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.ResponseResult;
import com.woniuxy.entity.Orders;
import com.woniuxy.mapper.OrdersMapper;
import com.woniuxy.service.OrdersService;
import com.woniuxy.utils.MapParse2Objutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单 服务实现类
 */

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    OrdersMapper ordersMapper;


    /**
     * 生成订单
     *
     * @param ordermap
     */
    @Override
    public void addOrders(Map<String, Object> ordermap) {

        Orders as = MapParse2Objutil.parse2Object(ordermap, Orders.class);

        ordersMapper.insert(as);
    }

    /**
     * 删除订单(软删除)
     *
     * @param ordersId
     */
    @Override
    public int updateOrders(String ordersId) {

        Orders orders = new Orders();

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("delete_state", 1);
        updateWrapper.or();
        updateWrapper.eq("order_id", ordersId);
        int update = ordersMapper.update(orders, updateWrapper);
        return update;
    }

    /**
     * 查询全部订单信息
     *
     * @return
     */
    @Override
    public List<Orders> queryAllOrders() {


        return ordersMapper.selectList(null);
    }

    /**
     * 根据订单号查询订单详情
     *
     * @param ordersId
     * @return
     */
    @Override
    public Orders queryOrdersOne(String ordersId) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", ordersId);
        Orders orders = ordersMapper.selectOne(queryWrapper);
        return orders;
    }


    /**
     * 根据订单号修改配送状态为  1:已接单
     * <p>
     * 0:未配送
     * 1:已接单
     * 2:取货中
     * 3:配送中
     * 4:已送达
     * 5:已超时
     *
     * @param ordersId
     * @return
     */
    @Override
    public int updateDispatchingStatus(String ordersId, int state) {

        Orders orders = new Orders();

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("dispatching_status", state);
        updateWrapper.or();
        updateWrapper.eq("order_id", ordersId);
        int uds = ordersMapper.update(orders, updateWrapper);


        return uds;
    }

    /**
     * 根据用户账号查询订单
     *
     * @param account
     * @return
     */
    @Override
    public List<Orders> findOrdersByUserAccount(String account) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", account);
        List<Orders> a = ordersMapper.selectList(queryWrapper);
        return a;
    }

    @Override
    public void insertRiderMessage() {

    }


}
